define(function(require, exports, module){

	require('{component}popup/popup.css');
	var popup = require('{component}popup/popup'),
		mId = 'recommended',listData,
		tpl = require("{view}recommended.tpl"),
		tplList = require("{view}recommendedList.tpl"),
		container = require("{component}container/container"),
		tplContainer = require("{view}container.tpl"),
	view = {

		template: _.template(tpl),
		templateList: _.template(tplList),
		templateContainer: _.template(tplContainer),

		render:function(){
			var parameter = share.getParameter(),
				songid = decodeURI(parameter.songid);
			window.scroll(0,0);
			
			if(songid==undefined || $('#'+mId).attr('songid') != songid){
				$("#"+mId).remove();
				$("body").append(this.template());
				share.container(mId,container,this.templateContainer,'top');
				this.ajaxSearch(songid);

			}else{
				// share.showPage(mId);
			}
			share.showPage(mId);
			this.reload();
		},
		reload:function(){
			var dom = $('#'+mId);
			container.reload(mId);

		},
		bindEvt:function(){
			var tthis = this;
			var dom = $("#"+mId);

			dom.find('.container_footer .tj').on('tap',function(){
				window.location.href = '#found';
			})

			dom.find('input.search').on('keydown',function(e){
				if(e.keyCode==13){
					window.location.href = '#search?q='+$(this).val();
				}
			});

			dom.find('.ico_music_animate').on('tap',function(){
				var songid = $('#play').attr('songid') || 877578;
				window.location.href = '#play?songid='+songid;
			})

			dom.find('.body_content').on('tap',function(e){
				var target = $(e.target),
					curLi = target.closest('li');
				if(curLi.attr('songid')){
					var songid = curLi.attr('songid');
					util.setLocalStorage('play_list',listData.result);
					window.location.href = '#play?songid='+songid;
				}
			});

			dom.find('li .ico_more').on('tap',function(e){
				var songid = $(this).parents('li').attr('songid');
				var tinguid = $(this).parents('li').attr('tinguid');
				e.stopPropagation();
				popup.show({type:'more',songid:songid,tinguid:tinguid,li:['recommended','singer']});

			});

			dom.find('input.search').on('keydown',function(e){
				if(e.keyCode==13){
					var val = $(this).val(),newArr=[];
					var history = util.getLocalStorage('search_history');
					if(history){
						history.unshift(val);
						newArr = history;
					}else{
						newArr.unshift(val);
					}

					util.setLocalStorage('search_history',newArr)
					window.location.href = '#search?q=' + val;
				}
			});

		},
		ajaxSearch:function(songid){
			share.loading();
			var tthis = this,dom=$('#'+mId);
			$.ajax(share.ajax({
				url: apiUrl,
				data:{method:'baidu.ting.song.getRecommandSongList',song_id:songid,num:50},
				success:function(data){
					listData = data;
					console.log(data)
			
					$('#'+mId).attr({'songid':songid});
					dom.find('.body_content').html(tthis.templateList(data));
					tthis.bindEvt();
					tthis.reload();
					share.cLoading('hide');
				}
			}))
		},
		
	};

	view.ajaxBool = true;
	view.userNav = false;
	module.exports =  view;
});

